using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using BL;
using Telerik.WinControls.UI;
using BO;

namespace AnimalFarm.Product_manage
{
    public partial class ListMilkTreatment : Telerik.WinControls.UI.RadForm
    {
        B_MilkTreatment B_milkTreatment = new B_MilkTreatment();
        B_TreatmentType B_treatType = new B_TreatmentType();
        B_Building B_building = new B_Building();
        B_Mix B_mix = new B_Mix();

        int milkTreatmentID, mixID;
        public ListMilkTreatment()
        {
            InitializeComponent();
        }

        //load milk treatment
        private void loadMilkTreatment()
        {
            DataTable aTable = B_milkTreatment.GetAllMilkTreatment();
            grvMilkTreatment.DataSource = aTable;
        }

        //load treatment type
        private void loadTreatType(int treatmentType)
        {
            DataTable aTable = B_treatType.GetAllTreatmentType();
            drdTreatmentType.DataSource = aTable;
            drdTreatmentType.DisplayMember = "TreatmentTypeName";
            drdTreatmentType.ValueMember = "TreatmentTypeID";
            drdTreatmentType.SelectedValue = treatmentType;
        }

        //load building
        private void loadBuilding(int buildingID)
        {
            DataTable aTable = B_building.GetAllBuilding();
            drdBuilding.DataSource = aTable;
            drdBuilding.DisplayMember = "BuildingName";
            drdBuilding.ValueMember = "BuildingID";
            drdBuilding.SelectedValue = buildingID;
        }

        private void ListMilkTreatment_Load(object sender, EventArgs e)
        {
            loadMilkTreatment();
            loadContextMenu();
        }

        //load context menu
        private void loadContextMenu()
        {
            ContextMenu menu = new ContextMenu();
            MenuItem item = new MenuItem();
            item.Text = "Delete";
            menu.MenuItems.Add(item);
            grvMilkTreatment.ContextMenu = menu;
            item.Click += new EventHandler(item_Click);
        }

        void item_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show(this, "Are you sure", "Deleting miltreatment", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                int milkTreatment = int.Parse(grvMilkTreatment.SelectedRows[0].Cells["MilkTreatmentID"].Value.ToString());
                bool isOk = B_milkTreatment.RemoveOneMilkTreatment(milkTreatmentID);
                if (isOk)
                {
                    MessageBox.Show(this, "Miltreatment deleted successful", "Miltreatment deleting", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    loadMilkTreatment();
                }
                else
                {
                    MessageBox.Show(this, "Can not delete this Miltreatment", "Miltreatment deleting", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }


        private void grvMilkTreatment_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvMilkTreatment.SelectedRows.Count > 0)
                {
                    GridViewRowInfo row = grvMilkTreatment.SelectedRows[0];
                    milkTreatmentID = int.Parse(row.Cells["MilkTreatmentID"].Value.ToString());
                    mixID = int.Parse(row.Cells["MixtID"].Value.ToString());
                    int treatmentTypeID = int.Parse(row.Cells["TreatmentTypeID"].Value.ToString());
                    DateTime time = DateTime.Parse(row.Cells["Time"].Value.ToString());
                    int buildingID = int.Parse(row.Cells["TreatmentLocationID"].Value.ToString());
                    string description = row.Cells["Description"].Value.ToString();

                    Mix_Info info = B_mix.GetOneMix(mixID);
                    txtMixName.Text = info.MixName;
                    loadTreatType(treatmentTypeID);
                    dtpTime.Value = time;
                    loadBuilding(buildingID);
                    txtDescriptioin.Text = description;
                }
            }
            catch (Exception ex) { }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btnSaveChange_Click(object sender, EventArgs e)
        {
            try
            {
                MilkTreatment_Info info = new MilkTreatment_Info();
                info.Description = txtDescriptioin.Text;
                info.MilkTreatmentID = milkTreatmentID;
                info.MixtID = mixID;
                info.Time = dtpTime.Value;
                info.TreatmentLocationID = int.Parse(drdBuilding.SelectedValue.ToString());
                info.TreatmentTypeID = int.Parse(drdTreatmentType.SelectedValue.ToString());

                bool isOk = B_milkTreatment.EditOneMilkTreatment(info);
                if (isOk)
                {
                    MessageBox.Show(this, "Milk treatment updated successful", "Milk treatment updating", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    loadMilkTreatment();
                }
                else
                {
                    MessageBox.Show(this, "Milk treatment updating is fail", "Milk treatment updating", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex) { }
        }
    }
}
